Fractale Tree  0.5
Plot Tree Fractale
draw_tree.cpp
Go to the documentation of this file.
1 #include "draw_tree.h"
16 {
17  //Marquage a false le flag SVG --> Pas de creation de fichier svg
18  IsSaveSVG=false;
19 }
27 //Cree un fichier SVG
28 void Draw_Tree::Save_file_svg(QString namefile)
29 {
30  //Marquage a true le flag SVG --> Creation d'un fichier svg
31  IsSaveSVG=true;
32  file.setFileName(namefile);
33  // Ouverture du fichier .svg
34  if (file.open(QIODevice::WriteOnly | QIODevice::Text))
35  {
36  // Ecriture du fichier .svg
37  QTextStream out(&file);
38  out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
39  out << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"" << X_max-X_min+200 <<"\" height=\"" << Y_max-Y_min+200 << "\">\n";
40  // Fermeture en ecriture du fichier .svg
41  file.close();
42  //Calcul des branches
43  Draw_Graph();
44  //Ecriture de la fin du fichier
45  if(file.open(QIODevice::Append | QIODevice::Text))
46  {
47  QTextStream out(&file);
48  out << "</svg>";
49  file.close();
50  }
51 
52 
53  }
54  //Marquage a false le flag SVG --> Pas de creation de fichier svg
55  IsSaveSVG=false;
56 }
57 
66 void Draw_Tree::Init(QGraphicsScene *scene, QProgressBar *ProgressBar_Plot)
67 {
69  ProgressBar=ProgressBar_Plot;
70 }
80 {
81  // Si IsSaveSVG=false --> Init les liniltes de l'abre
82  if(!IsSaveSVG)
83  {
84  X_max=0;
85  X_min=0;
86  Y_max=0;
87  Y_min=0;
88  }
89  // Init frame --> X=0 Y=0 d=l alpha = -90°
90  frame.Init(0,0,lenght,-90);
91 
92  //Init scene
93  scene->clear();
94 }
95 
107 void Draw_Tree::Plot(QWidget_trees *Widget_trees,QWidget_Config *Widget_Config,QWidget_Ratio *Widget_Ratio,QWidget_Graphique *Widget_Graphique)
108 {
109  //Init
110  stock_prcent=Widget_trees->Get_Stock_Prcent();
111 
112  niveau=Widget_Config->Get_Niveau();
113  niveau_max=Widget_Config->Get_Niveau();
114 
115  lenght=Widget_Config->Get_Lenght();
116  sizePen=Widget_Config->Get_SizePen();
117 
118  prcent_lenght=Widget_Ratio->Get_Ratio()/100;
119  etat_ratio=Widget_Ratio->isChecked();
120 
121  color=Widget_Graphique->Get_Color();
122  color_auto=Widget_Graphique->Get_Color_Auto();
123 
124  with_bout=Widget_Graphique->Get_with_Bout();
125 
126  Draw_Graph();
127 
128  //Init
129  stock_prcent=Widget_trees->Get_Stock_Prcent();
130 
131  niveau=Widget_Config->Get_Niveau();
132  niveau_max=Widget_Config->Get_Niveau();
133 
134  lenght=Widget_Config->Get_Lenght();
135  sizePen=Widget_Config->Get_SizePen();
136 
137  prcent_lenght=Widget_Ratio->Get_Ratio()/100;
138  etat_ratio=Widget_Ratio->isChecked();
139 
140  color=Widget_Graphique->Get_Color();
141  color_auto=Widget_Graphique->Get_Color_Auto();
142 
143  with_bout=Widget_Graphique->Get_with_Bout();
144 }
152 {
153  // Clear();
154  Clear();
155  //base
156  Draw_Line();
157  frame.Move();
158  //draw
159  Frame frame_loc=frame;
160 
161  double lenght_loc=lenght;
162  double sizePen_loc=sizePen;
163  int nb_branche=0;
164 
165  // Calcul du nombre de branche
166  for(int i=0;i<stock_prcent.Get_size();i++)
167  {
169  nb_branche++;
170  }
171 
172  //Calcul le nombre de branche
173  int nb_max=0;
174  for(int i=1;i<=niveau_max;i++)
175  {
176  nb_max=nb_max+(int)qPow(nb_branche,i);
177  }
178 
179  //Parametre la ProgressBar avec 0 --> nb de branche
180  ProgressBar->setMaximum(nb_max);
181  ProgressBar->setValue(0);
182  if(niveau_max==0)
183  {
184  ProgressBar->setMaximum(100);
185  ProgressBar->setValue(ProgressBar->maximum());
186  }
187  // Si couleur Auto
188  if(color_auto)
189  {
190  //
191  for(int i=0;i<=niveau_max;i++)
192  {
193  frame=frame_loc;
194  sizePen=sizePen_loc;
195  lenght=lenght_loc;
196  niveau=i;
197  Sub_Draw();
198  }
199  }
200  else
201  {
202  Sub_Draw();
203  }
204 }
205 
207 {
208 
209  Frame frame_loc = frame;
210  double lenght_loc=lenght;
211  double sizePen_loc=sizePen;
212 
213  frame_loc.Add_alpha(90);
214  Frame frame_temp = frame_loc;
215  niveau--;
216  if(niveau>-1)
217  {
218  double angle=0;
219  double angle_temp=0;
220  for(int i=0;i<stock_prcent.Get_size();i++)
221  {
222  angle_temp=stock_prcent.Get_Angle_By_Index(i);
223  if(angle_temp>0)
224  {
225  frame_temp=frame_loc;
226 
227  double l=qCos(angle_temp/2*PI/180)*sizePen_loc/2;
228  double d=qSin(angle_temp/2*PI/180)*sizePen_loc/2;
229 
230  frame_temp.Add_alpha(-angle-angle_temp/2);
231  frame_temp.Set_d(l);
232  frame_temp.Move();
233 
234  sizePen=d*2;
235  if(etat_ratio)
236  lenght=lenght_loc*prcent_lenght;
237  else
238  lenght=sizePen;
239  frame=frame_temp;
240  frame.Set_d(lenght);
241 
242  if(niveau==0 || !color_auto)
243  {
244  Draw_Line();
245  ProgressBar->setValue(ProgressBar->value()+1);
246  }
247 
248 
249  frame.Move();
250  Sub_Draw();
251 
252  angle=angle+angle_temp;
253 
254  }
255  }
256 
257  }
258  niveau++;
259 }
261 {
262  //qDebug() << "1 sizePen="<< sizePen;
263  double x1,x2,y1,y2;
264  x1=frame.Get_x1();
265  x2=frame.Get_x2();
266  y1=frame.Get_y1();
267  y2=frame.Get_y2();
268  //X_max
269  if(x1>X_max)
270  {
271  X_max=x1;
272  }
273  if(x2>X_max)
274  {
275  X_max=x2;
276  }
277  //X_min
278  if(x1<X_min)
279  {
280  X_min=x1;
281  }
282  if(x2<X_min)
283  {
284  X_min=x2;
285  }
286  //Y_max
287  if(y1>Y_max)
288  {
289  Y_max=y1;
290  }
291  if(y2>Y_max)
292  {
293  Y_max=y2;
294  }
295  //Y_min
296  if(y1<Y_min)
297  {
298  Y_min=y1;
299  }
300  if(y2<Y_min)
301  {
302  Y_min=y2;
303  }
304 
305 
306  Frame frame_temp;
307  QVector<QPointF> VectorQPointF;
308 
309  //P1
310  frame_temp=frame;
311  frame_temp.Add_alpha(90);
312  frame_temp.Set_d(sizePen/2);
313  VectorQPointF.append(QPointF(frame_temp.Get_x2(),frame_temp.Get_y2()));
314 
315  //P2
316  frame_temp=frame;
317  frame_temp.Move();
318  frame_temp.Add_alpha(90);
319  frame_temp.Set_d(sizePen/2);
320  VectorQPointF.append(QPointF(frame_temp.Get_x2(),frame_temp.Get_y2()));
321 
322  //P3
323  frame_temp=frame;
324  frame_temp.Move();
325  frame_temp.Add_alpha(-90);
326  frame_temp.Set_d(sizePen/2);
327  VectorQPointF.append(QPointF(frame_temp.Get_x2(),frame_temp.Get_y2()));
328 
329  //P4
330  frame_temp=frame;
331  frame_temp.Add_alpha(-90);
332  frame_temp.Set_d(sizePen/2);
333  VectorQPointF.append(QPointF(frame_temp.Get_x2(),frame_temp.Get_y2()));
334  //QVector<QPointF>
335  //QPolygonF()
336  QPolygonF PolygonF=QPolygonF(VectorQPointF);
337  //qDebug() << "2 sizePen="<< sizePen;
338  QColor Color ;
339  Color=color;
340  if(color_auto)
341  {
342  Color.setRed(qrand()%255);
343  Color.setGreen(qrand()%255);
344  Color.setBlue(qrand()%255);
345  }
346 
347  QPen Pen(Color);
348  //Pen.setWidth((int)sizePen);
349  Pen.setWidth(0);
350 
351  QBrush Brush(Color);
352 
353  scene->addPolygon(PolygonF,Pen,Brush);
354 
355  if(with_bout)
356  {
357  double X1,X2,Y1,Y2;
358  X1=x2-(sizePen/2);
359  Y1=y2-(sizePen/2);
360  X2=x2+(sizePen/2);
361  Y2=y2+(sizePen/2);
362  double dX,dY;
363  dX=X2-X1;
364  dY=Y2-Y1;
365  scene->addEllipse(X1,Y1,dX,dY,Pen,Brush);
366  }
367  //qDebug() << "3 sizePen="<< sizePen;
368  //SVG
369  //double x1,x2,y1,y2;
370  x1=frame.Get_x1()-X_min+100;
371  x2=frame.Get_x2()-X_min+100;
372  y1=frame.Get_y1()-Y_min+100;
373  y2=frame.Get_y2()-Y_min+100;
374 
375  if(IsSaveSVG)
376  {
377  if(file.open(QIODevice::Append | QIODevice::Text))
378  {
379  QTextStream out(&file);
380  out << "<line x1=\"" << x1 << "\" y1=\"" << y1 << "\" x2=\"" << x2 << "\" y2=\"" << y2 << "\" stroke=\"rgb(" << Color.red() <<"," << Color.green() << "," << Color.blue() << ")\" stroke-width=\"" << (int)sizePen << "\"/>\n";
381  if(with_bout)
382  {
383  out << "<circle cx=\"" << x2 <<"\" cy=\"" << y2 << "\" r=\"" << sizePen/2 << "\" fill=\"rgb(" << Color.red() <<"," << Color.green() << "," << Color.blue() << ")\"/> \n";
384  }
385  file.close();
386  }
387  //qDebug() << "4 sizePen="<< sizePen;
388  }
389 
390 }
bool color_auto
Definition: draw_tree.h:78
double Get_y1()
Definition: frame.cpp:46
void Draw_Line()
Definition: draw_tree.cpp:260
double Y_min
Definition: draw_tree.h:93
void Draw_Graph()
Desiner l&#39;abre.
Definition: draw_tree.cpp:151
void Clear()
Clear la class.
Definition: draw_tree.cpp:79
QProgressBar * ProgressBar
Definition: draw_tree.h:95
double X_min
Definition: draw_tree.h:92
QFile file
Definition: draw_tree.h:87
double Get_x1()
Definition: frame.cpp:41
void Set_d(double d)
Definition: frame.cpp:17
bool IsSaveSVG
Definition: draw_tree.h:81
double Get_SizePen()
double Get_Angle_By_Index(int index)
bool with_bout
Definition: draw_tree.h:79
void Move()
Definition: frame.cpp:22
#define PI
Definition: frame.h:5
Frame frame
Definition: draw_tree.h:69
QColor color
Definition: draw_tree.h:77
void Sub_Draw()
Definition: draw_tree.cpp:206
int niveau_max
Definition: draw_tree.h:72
lib affichage de l&#39;arbre
double Y_max
Definition: draw_tree.h:91
double sizePen
Definition: draw_tree.h:74
Stock_Prcent stock_prcent
Definition: draw_tree.h:85
QGraphicsScene * scene
Definition: draw_tree.h:83
Draw_Tree()
Constructeur de la classe.
Definition: draw_tree.cpp:15
double Get_y2()
Definition: frame.cpp:56
void Plot(QWidget_trees *Widget_trees, QWidget_Config *Widget_Config, QWidget_Ratio *Widget_Ratio, QWidget_Graphique *Widget_Graphique)
Desiner l&#39;arbre.
Definition: draw_tree.cpp:107
double Get_x2()
Definition: frame.cpp:51
double X_max
Definition: draw_tree.h:90
Stock_Prcent Get_Stock_Prcent()
void Init(double x, double y, double d, double alpha)
Definition: frame.cpp:4
double lenght
Definition: draw_tree.h:73
lib gestion de repère 2D
Definition: frame.h:17
void Save_file_svg(QString namefile)
Cree un fichier SVG.
Definition: draw_tree.cpp:28
int niveau
Definition: draw_tree.h:71
bool etat_ratio
Definition: draw_tree.h:76
double prcent_lenght
Definition: draw_tree.h:75
Widget de configuration d&#39;affichage des niveaus des branches.
double Get_Ratio()
double Get_Prcent_By_Index(int index)
void Init(QGraphicsScene *scene, QProgressBar *ProgressBar_Plot)
Initialise la class.
Definition: draw_tree.cpp:66
void Add_alpha(double alpha)
Definition: frame.cpp:12